//
// Created by Administrator on 25-5-17.
//

#ifndef USK_LDAP_FILTER_HPP
#define USK_LDAP_FILTER_HPP
#include <string>

#include "Poco/AccessExpireLRUCache.h"
#include "cppmicroservices/LDAPFilter.h"
#include "cppmicroservices/em/EMConstants.hpp"

// C++ CRTP


class USKCachedTopicFilter {
public:
    using LDAPFilterPtr = std::shared_ptr<cppmicroservices::LDAPFilter>;
    USKCachedTopicFilter() {}

    LDAPFilterPtr CreateTopicFilter(const std::string &topic) {
        if (ldap_cache_.has(topic)) {
            auto record = ldap_cache_.get(topic);
            return *record.get();
        }
        std::stringstream ss;
        ss << "(|(" << cppmicroservices::em::Constants::EVENT_TOPIC << "=" << topic << ")("
           << cppmicroservices::em::Constants::EVENT_TOPIC << "=" << topic << "/*))";
        std::string filter_string = ss.str();

        auto filter = std::make_shared<cppmicroservices::LDAPFilter>(filter_string);
        ldap_cache_.add(topic, filter);
        return filter;
    }

private:
    Poco::AccessExpireLRUCache<std::string, LDAPFilterPtr> ldap_cache_;
};


#endif // USK_LDAP_FILTER_HPP
